package com.atguigu.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author 孙志鹏
 * @createDate 2022/4/5 16:02
 */
@Service
public class PaymentService {

    public String paymentInfoOK(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "payment_ok,id: " + id + "\t" + "O(∩_∩)O";
    }

    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler",
            commandProperties = {
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="1000")
            }
    )
    public String paymentInfoTimeout(Integer id) throws InterruptedException {
        Thread.sleep(2000);
        return "线程池: " + Thread.currentThread().getName() + "payment_timeout,id" + id + "\t" + "O(∩_∩)O";
    }

    public String paymentInfoTimeoutHandler(@PathVariable(value = "id") Integer id) {
        return "Hystrix降级超时保障方法：线程池: " + Thread.currentThread().getName() + "payment_timeout,id" + id;
    }
}
